﻿using System;
using Loki.IoC;
using Loki.Utils;

namespace Loki.Model
{
    public abstract class Entity : TrackedObject, IEntity, IContextAware
    {
        protected Entity()
            : base()
        {
        }

        protected Entity(IEntity clone)
            : base(clone)
        {
        }

        private IObjectCreator context;

        public IObjectCreator Context
        {
            protected get
            {
                if (context == null)
                {
                    return Toolkit.IoC.DefaultContext;
                }
                else
                {
                    return context;
                }
            }

            set
            {
                context = value;
                OnContextInitialized(EventArgs.Empty);
            }
        }

        #region ContextInitialized

        public event EventHandler ContextInitialized;

        protected virtual void OnContextInitialized(EventArgs e)
        {
            EventHandler handler = ContextInitialized;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        #endregion ContextInitialized
    }
}